<!-- 搜索表单 -->
<template>
  <ele-card :body-style="{ paddingBottom: '2px' }">
    <el-form label-width="80px" @keyup.enter="search">
      <el-row :gutter="8">
        <el-col :lg="6" :md="8" :sm="12" :xs="24">
          <el-form-item label="订单信息">
            <el-input
              v-model.trim="form.keyword"
              clearable
              placeholder="请输入"
            />
          </el-form-item>
        </el-col>
        <el-col :lg="6" :md="8" :sm="12" :xs="24">
          <el-form-item label="商品信息">
            <el-input
              v-model.trim="form.product"
              clearable
              placeholder="请输入"
            />
          </el-form-item>
        </el-col>
        <el-col :lg="6" :md="8" :sm="12" :xs="24">
          <el-form-item label="货主">
            <el-select v-model="form.supplier_id" placeholder="请选择货主" clearable @change="supplierChange">
              <el-option v-for="item in supplierList" :key="item.id" :value="item.id" :label="item.name" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :lg="6" :md="8" :sm="12" :xs="24">
          <el-form-item label="仓库">
            <el-select v-model="form.warehouse_id" :disabled="!form.supplier_id" placeholder="请选择仓库" clearable>
              <el-option v-for="item in warehouseList" :key="item.id" :value="item.id" :label="item.name" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :lg="6" :md="8" :sm="12" :xs="24">
          <el-form-item label="单据状态">
            <el-select v-model="form.status" placeholder="请选择状态" clearable>
              <el-option v-for="item in statusList.filter(v => v.id)" :key="item.id" :value="item.id" :label="item.name" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :lg="6" :md="8" :sm="12" :xs="24">
          <el-form-item label="入库类型">
            <el-select v-model="form.type" placeholder="请选择入库类型" clearable>
              <el-option v-for="item in typeList.filter(v => v.id)" :key="item.id" :value="item.id" :label="item.name" />
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :lg="6" :md="8" :sm="12" :xs="24">
          <el-form-item label="单据日期">
            <el-date-picker
              v-model="form.dateRange"
              unlink-panels
              type="daterange"
              range-separator="-"
              value-format="YYYY-MM-DD"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              class="ele-fluid"
            />
          </el-form-item>
        </el-col>

        <el-col :lg="6" :md="8" :sm="24" :xs="24">
          <el-form-item label-width="16px">
            <el-button type="primary" @click="search">
              查询
            </el-button>
            <el-button @click="reset">
              重置
            </el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </ele-card>
</template>

<script setup>
import { listBanWarehouse } from '@/api/common'
import { useFormData } from '@/utils/use-form-data'

defineProps({
  supplierList: Array,
})

const emit = defineEmits(['search'])

/** 表单数据 */
const { form, resetFields } = useFormData({
  keyword: '',
  product: '',
  status: '',
  type: '',
  warehouse_id: undefined,
  supplier_id: undefined,
  dateRange: [],
  start_date: '',
  end_date: '',
})

const [statusList] = utils.useGetList('/common/enterStatus')
const [typeList] = utils.useGetList('/common/enterType')

const warehouseList = ref([])

async function supplierChange(value) {
  if (value) {
    const res = await listBanWarehouse(value)
    warehouseList.value = res.data
  }
  else {
    warehouseList.value = []
  }
  if (form.warehouse_id && !warehouseList.value.some(v => v.id === form.warehouse_id)) {
    form.warehouse_id = ''
  }
}

/** 搜索 */
function search() {
  const [start_date, end_date] = form.dateRange ?? []
  emit('search', {
    ...form,
    dateRange: undefined,
    start_date,
    end_date,
  })
}

/**  重置 */
function reset() {
  resetFields()
  search()
}
</script>
